package com.hyphenate.easeui.widget.chatrow;

import android.content.Context;
import android.text.Spannable;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TextView.BufferType;

import com.google.gson.Gson;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMMessage.ChatType;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.easeui.R;
import com.hyphenate.easeui.model.ChatBean;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.hyphenate.easeui.widget.TextViewFixTouchConsume;
import com.hyphenate.exceptions.HyphenateException;

public class EaseChatRowText extends EaseChatRow{

	private TextViewFixTouchConsume contentView;
    private ImageView vip;

    public EaseChatRowText(Context context, EMMessage message, int position, BaseAdapter adapter) {
		super(context, message, position, adapter);
	}

	@Override
	protected void onInflatView() {
		inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?
				R.layout.ease_row_received_message : R.layout.ease_row_sent_message, this);
	}

	@Override
	protected void onFindViewById() {
		contentView = (TextViewFixTouchConsume) findViewById(R.id.tv_chatcontent);
//        vip = (ImageView) findViewById(R.id.vip);
//        if ("-1".equals(message.getType())) {
//            vip.setVisibility(View.GONE);
//        } else
//        if ("1".equals(message.getType())) {
//            vip.setVisibility(View.VISIBLE);
//            vip.setImageResource(R.drawable.v_huan);
//        } else
//        if ("0".equals(message.getType())) {
//            vip.setVisibility(View.VISIBLE);
//            vip.setImageResource(R.drawable.v_lan);
//        }
//        else
//            vip.setVisibility(View.GONE);

	}

    @Override
    public void onSetUpView() {
        EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();
        if(null==urlIntent) {
            Spannable span = EaseSmileUtils.getSmiledText(context, txtBody.getMessage());
            // 设置内容
            contentView.setText(span, BufferType.SPANNABLE);
        }else {
            contentView.setText(txtBody.getMessage(), urlIntent);
            contentView.setMovementMethod(TextViewFixTouchConsume.LocalLinkMovementMethod.getInstance());
        }
                handleTextMessage();
        if(" ".equals(txtBody.getMessage())){
            setVisibility(GONE);
        }else{
            setVisibility(VISIBLE);
        }
        String str= null;
        try {
            str = message.getStringAttribute("userinfo");
            ChatBean chatBean=new Gson().fromJson(str,ChatBean.class);
            if (chatBean.getImid().equals("groupadmin")){
                TextView groupadmin = (TextView) findViewById(R.id.groupadmin);
                findViewById(R.id.message_layout).setVisibility(GONE);
                findViewById(R.id.timestamp).setVisibility(GONE);
                groupadmin.setVisibility(VISIBLE);
                groupadmin.setText(txtBody.getMessage());
            } else {
                findViewById(R.id.message_layout).setVisibility(VISIBLE);
                findViewById(R.id.groupadmin).setVisibility(GONE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void handleTextMessage() {
        if (message.direct() == EMMessage.Direct.SEND) {
            setMessageSendCallback();
            switch (message.status()) {
            case CREATE: 
                progressBar.setVisibility(View.GONE);
                statusView.setVisibility(View.VISIBLE);
                // 发送消息
//                sendMsgInBackground(message);
                break;
            case SUCCESS: // 发送成功
                progressBar.setVisibility(View.GONE);
                statusView.setVisibility(View.GONE);
                break;
            case FAIL: // 发送失败
                progressBar.setVisibility(View.GONE);
                statusView.setVisibility(View.VISIBLE);
                break;
            case INPROGRESS: // 发送中
                progressBar.setVisibility(View.VISIBLE);
                statusView.setVisibility(View.GONE);
                break;
            default:
                progressBar.setVisibility(View.GONE);
                statusView.setVisibility(View.GONE);
               break;
            }
        }else{
            if(!message.isAcked() && message.getChatType() == ChatType.Chat){
                try {
                    EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    protected void onUpdateView() {
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onBubbleClick() {
        // TODO Auto-generated method stub
        
    }



}
